package com.gzck.gzck_verson2.service.impl;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzck.gzck_verson2.mapper.GFactorMapper;
import com.gzck.gzck_verson2.pojo.GProjectInfo;
import com.gzck.gzck_verson2.mapper.GProjectInfoMapper;
import com.gzck.gzck_verson2.pojo.GProjectType;
import com.gzck.gzck_verson2.pojo.page.MyPage;
import com.gzck.gzck_verson2.service.IGProjectInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 工程信息 服务实现类
 * </p>
 *
 * @author 任永辉
 * @since 2024-01-06
 */
@Service
public class GProjectInfoServiceImpl extends ServiceImpl<GProjectInfoMapper, GProjectInfo> implements IGProjectInfoService {
    @Autowired
    GProjectInfoMapper gProjectInfoMapper;

    /**
     * 添加工程
     *
     * @param projectInfo
     * @return
     */
    @Override
    @Transactional
    public SaResult saveProjectInfo(GProjectInfo projectInfo) {
        //先判断是否有重复的工程名称
        String addvName = projectInfo.getAddvName();
        GProjectInfo one = this.getOne(
                Wrappers.lambdaQuery(GProjectInfo.class).eq(GProjectInfo::getAddvName, addvName)
        );
        if (ObjectUtil.isNotEmpty(one)){
            return SaResult.error("工程已存在");
        }
        //工程没有创建过，就创建新工程
        boolean save = this.save(projectInfo);
        return SaResult.ok("工程添加成功");
    }

    /**
     * 条件分页查询
     * @param
     * @return
     */
    @Override
    public IPage<GProjectInfo> pageByCondition(MyPage<GProjectInfo> pageInfo) {
        Page<GProjectInfo> page = pageInfo.getPage();

        Page<GProjectInfo> pages = page(page,
        Wrappers.lambdaQuery(GProjectInfo.class)
                //工程名称
                .eq(ObjectUtil.isNotNull(pageInfo.getCondition().getProjectDesc()),GProjectInfo::getProjectDesc, pageInfo.getCondition().getName())
                //工程类型
                .eq(ObjectUtil.isNotNull(pageInfo.getCondition().getAddvName()),GProjectInfo::getAddvName, pageInfo.getCondition().getType())
                //工程负责人
                .eq(ObjectUtil.isNotNull(pageInfo.getCondition().getOwner()),GProjectInfo::getOwner, pageInfo.getCondition().getOwner())
                //纬度
                .eq(ObjectUtil.isNotNull(pageInfo.getCondition().getLttd()),GProjectInfo::getLttd, pageInfo.getCondition().getLttd())
                //精度
                .eq(ObjectUtil.isNotNull(pageInfo.getCondition().getLgtd()),GProjectInfo::getLgtd, pageInfo.getCondition().getLgtd())
                //高程
                .eq(ObjectUtil.isNotNull(pageInfo.getCondition().getAlti()),GProjectInfo::getAlti, pageInfo.getCondition().getAlti())
                //工程状态
                .eq(ObjectUtil.isNotNull(pageInfo.getCondition().getStatus()),GProjectInfo::getStatus, pageInfo.getCondition().getStatus())
                //行业id
                .eq(ObjectUtil.isNotNull(pageInfo.getCondition().getIndustryId()),GProjectInfo::getIndustryId, pageInfo.getCondition().getIndustryId())
                //根据时间范围查询
                .between(ObjectUtil.isNotNull(pageInfo.getCondition().getCtime()),GProjectInfo::getUptime, pageInfo.getCondition().getCtime(),pageInfo.getCondition().getUptime())
                );
        return pages;
    }
}
